<?php

/**
 * Aurorum - Open source digital signage
 *
 * @link http://digitalsignage.tk
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 * @category Aurorum
 * @package Aurorum
 */

/**
 * This class transmits requested stuff to client.
 *
 * @package Aurorum
 * @subpackage Core
 */

class ADS_Deployment{
    
    private $swVersion = '0.0.0';
    
    public function __construct($swVersion) {
        $this->swVersion = $swVersion;
    }
    
    public function buildDescriptor($format = 'xml', $revision = 0){
        $descriptor = array(
            'head'   => array(),
            'frames' => array(
                'slides' => array(),
                'data'   => array(),
            ),
            'datasources' => array(),
        );
        $sModel  = new ADS_SlideModel();        
        $siModel = new ADS_SlideItemModel();        
        foreach($sModel->setSorter(array('position' => 'ASC'))->getSlides() as $slide){
            $_slide = array(
                'id'  => 's'.$slide->slideId,
                'url' => $slide->guid
            );
            //data frames
            foreach($siModel->getItems((int)$slide->slideId) as $zone) {
                $_slide['zones'][] = array(
                    'frameId' => 'df'.$zone->frameId,
                    'htmlId'  => $zone->zoneId                    
                );
            }
            $descriptor['frames']['slides'][] = $_slide;
        }
        
        //data frames
        $dfModel  = new ADS_DataFrameModel();
        $dfiModel = new ADS_DataFrameItemModel();
        foreach($dfModel->getFrames() as $frame){
            $dataframe = array(
                'id'       => 'df'.$frame->frameId,
                'name'     => $frame->name,
                'timeline' => array()
            );            
            //data sources
            foreach($dfiModel->setSorter(array('position' => 'ASC'))->getItems((int)$frame->frameId) as $source) {
                $dataframe['timeline'][] = array(
                    'id' => 'ds'.$source->sourceId
                );
            }
            $descriptor['frames']['data'][] = $dataframe;
        }
        //data sources
        $dsModel = new ADS_DataSourceModel();        
        $wpModel = new ADS_WPModel();        
        foreach($dsModel->getSources() as $source){
            $datasource = array(
                'id'       => 'ds'.$source->sourceId,
                'content'  => $source->content,
                'timeline' => array()
            );  
            $dsiModel = new ADS_DataSourceItemModel($source->content,$source->type);
            //items
            foreach($dsiModel->setSorter(array('position' => 'ASC'))->getItems((int)$source->sourceId) as $item) {
                switch($source->type){                    
                    case 'item':
                        $datasource['timeline'][] = array(
                            'url'       => $item->guid,
                            'duration'  => $item->duration*1000,
                            'validFrom' => $item->validFrom,
                            'validTo'   => $item->validTo,
                            'mime'      => $item->mimeType
                        );
                    break;
                    
                    case 'tag':
                        foreach($wpModel->getPostsWithTag($source->content, (int)$item->targetId) as $wpItem){
                            $datasource['timeline'][] = array(
                                'url'       => $wpItem->guid,
                                'duration'  => $item->duration*1000,
                                'validFrom' => $item->validFrom,
                                'validTo'   => $item->validTo,
                                'mime'      => $item->post_mime_type
                            );
                        }
                    break;
                }                
            }
            $descriptor['datasources'][] = $datasource;
        }
        
        $descriptor['head']['revision']  = $revision;

        return json_encode($descriptor);
    }
    
}
